package com.intelligentmeterreadingsys.otherequipment.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.otherequipment.db.EquipementDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.QueryRoutePlanMap;

public class EquipementInfoActivity extends Activity {
    public static final int ITEM0 = Menu.FIRST;// 系统值
    public TextView textview1;// 设备编号
    public TextView textview2;// 安装地址
    public TextView textview3;// 设备类型
    public TextView textview4;// 安装日期
    public TextView textview5;// 设备描述
    public TextView textview6;// 巡检结果
    public TextView textview7;// 巡检状态
    public TextView textview8;// 结果描述
    public EquipementQueryActivityGroup act = null;// 父类
    public String equipementId = "";
    public OtherEquipmentPOVO equi = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.other_equipment_info);
	init();
	queryEquiment();
    }

    /**
     * 初始化控件
     */
    public void init() {
	textview1 = (TextView) findViewById(R.id.other_equipment_info_number);
	textview2 = (TextView) findViewById(R.id.other_equipment_info_adds);
	textview3 = (TextView) findViewById(R.id.other_equipment_info_type);
	textview4 = (TextView) findViewById(R.id.other_equipment_info_installation_date);
	textview5 = (TextView) findViewById(R.id.other_equipment_info_desc);
	textview6 = (TextView) findViewById(R.id.other_equipment_info_result);
	textview7 = (TextView) findViewById(R.id.other_equipment_info_checkstatus);
	textview8 = (TextView) findViewById(R.id.other_equipment_info_resultdesc);
    }

    /**
     * 查询并添加数据
     */
    public void queryEquiment() {
	act = (EquipementQueryActivityGroup) getParent();
	equipementId = act.getEquipmentid();
	if (equipementId == null || equipementId.equals("")) {
	    Toast.makeText(EquipementInfoActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
	    return;
	}
	String id = act.getEquipmentid();
	EquipementDao dao = new EquipementDao(this);
	equi = dao.queryOne(id, null);
	dao.close();
	textview1.setText("");
	textview2.setText("");
	textview3.setText("");
	textview4.setText("");
	textview5.setText("");
	textview6.setText("");
	textview7.setText("");
	textview8.setText("");
	if (equi != null) {
	    textview1.setText(equi.getNumber());
	    textview2.setText(equi.getAdds());
	    textview3.setText(equi.getTypename());
	    textview4.setText(equi.getDate());
	    textview5.setText(equi.getDesc());
	    textview6.setText(equi.getChecktype());
	    textview7.setText(equi.getIscheck());
	    textview8.setText(equi.getCheckdesc());
	}

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

	menu.add(0, ITEM0 + 1, 0, "更换设备");
	menu.add(0, ITEM0 + 2, 0, "设备巡检");
	menu.add(0, ITEM0 + 3, 0, "百度地图");
	menu.findItem(ITEM0 + 1);
	return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	if (equi == null) {
	    Toast.makeText(this, "没有设备信息不能操作此功能", Toast.LENGTH_SHORT).show();
	    return false;
	}
	Intent intent = null;
	if (item.getItemId() == ITEM0 + 3) {
	    if (equi.getLatitude() == 0.0) {
		Toast.makeText(this, "请先定位", Toast.LENGTH_SHORT).show();
		return false;
	    }
	    intent = new Intent(this, QueryRoutePlanMap.class);
	    intent.putExtra("wateradds", equi.getAdds());
	    intent.putExtra("latitude", equi.getLatitude());
	    intent.putExtra("longitude", equi.getLongitude());
	    intent.putExtra("adds", equi.getLocationadds());
	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 1)) {
	    intent = new Intent(this, EquipementReplaceActivity.class);
	    intent.putExtra("equipementId", equi.getId());
	    startActivity(intent);
	}
	return super.onOptionsItemSelected(item);
    }

}
